#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char** argv) {
	//创建子进程
	int result;
	result = fork();
	if (result < 0) {
	perror("Failed to create child");
	exit(1);
	} else if (result == 0) {
		// Child 1
	char *subprogram = argv[1];		//获取需要执行的程序名称
	char *subprogram_argv[argc];
	printf("argc:%d\n", argc);/*test*/
	for(int i=1;i<argc;i++){
		subprogram_argv[i-1]=argv[i];	//获取需要执行的程序的参数
		printf("%d: %s\n", i, argv[i]);/*test*/
	}
	subprogram_argv[argc-1]=NULL;
	
	printf("Child process's PID is %d. My parent's PID is %d.\n", getpid(), getppid());
	printf("Child process is about to execute \"%s\"\n\n", subprogram);
	result = execvp(subprogram, subprogram_argv);	//执行指定程序
	if (result == -1) {
		perror("In child process, failed to exec a program");
	}
	exit(1);
	} else {
		// parent
	int status;
	printf("Parent process's PID is %d.\n", getpid());	
	printf("Parent process is waiting ... \n");
	wait(&status);
	printf("In parent process, status = 0x%x, WEXITSTATUS(status) = %d (i.e. 0x%x)\n", \
	status, WEXITSTATUS(status), WEXITSTATUS(status));
	}
	return (EXIT_SUCCESS);
}
